home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / frantic.swf / scripts / fl / controls / Slider.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  12.9 KB  |  424 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.InteractionInputType;
  6.    import fl.events.SliderEvent;
  7.    import fl.events.SliderEventClickTarget;
  8.    import fl.managers.IFocusManagerComponent;
  9.    import flash.display.DisplayObject;
  10.    import flash.display.Sprite;
  11.    import flash.events.KeyboardEvent;
  12.    import flash.events.MouseEvent;
  13.    import flash.ui.Keyboard;
  14.    
  15.    [Embed(source="/_assets/assets.swf", symbol="fl.controls.Slider")]
  16.    public class Slider extends UIComponent implements IFocusManagerComponent
  17.    {
  18.       
  19.       protected static var defaultStyles:Object = {
  20.          "thumbUpSkin":"SliderThumb_upSkin",
  21.          "thumbOverSkin":"SliderThumb_overSkin",
  22.          "thumbDownSkin":"SliderThumb_downSkin",
  23.          "thumbDisabledSkin":"SliderThumb_disabledSkin",
  24.          "sliderTrackSkin":"SliderTrack_skin",
  25.          "sliderTrackDisabledSkin":"SliderTrack_disabledSkin",
  26.          "tickSkin":"SliderTick_skin",
  27.          "focusRectSkin":null,
  28.          "focusRectPadding":null
  29.       };
  30.       
  31.       protected static const TICK_STYLES:Object = {"upSkin":"tickSkin"};
  32.       
  33.       protected static const TRACK_STYLES:Object = {
  34.          "upSkin":"sliderTrackSkin",
  35.          "overSkin":"sliderTrackSkin",
  36.          "downSkin":"sliderTrackSkin",
  37.          "disabledSkin":"sliderTrackDisabledSkin"
  38.       };
  39.       
  40.       protected static const THUMB_STYLES:Object = {
  41.          "upSkin":"thumbUpSkin",
  42.          "overSkin":"thumbOverSkin",
  43.          "downSkin":"thumbDownSkin",
  44.          "disabledSkin":"thumbDisabledSkin"
  45.       };
  46.        
  47.       
  48.       protected var _direction:String;
  49.       
  50.       protected var _liveDragging:Boolean = false;
  51.       
  52.       protected var _value:Number = 0;
  53.       
  54.       protected var _snapInterval:Number = 0;
  55.       
  56.       protected var _minimum:Number = 0;
  57.       
  58.       protected var _maximum:Number = 10;
  59.       
  60.       protected var track:BaseButton;
  61.       
  62.       protected var _tickInterval:Number = 0;
  63.       
  64.       protected var tickContainer:Sprite;
  65.       
  66.       protected var thumb:BaseButton;
  67.       
  68.       public function Slider()
  69.       {
  70.          _direction = SliderDirection.HORIZONTAL;
  71.          _minimum = 0;
  72.          _maximum = 10;
  73.          _value = 0;
  74.          _tickInterval = 0;
  75.          _snapInterval = 0;
  76.          _liveDragging = false;
  77.          super();
  78.          setStyles();
  79.       }
  80.       
  81.       public static function getStyleDefinition() : Object
  82.       {
  83.          return defaultStyles;
  84.       }
  85.       
  86.       public function get minimum() : Number
  87.       {
  88.          return _minimum;
  89.       }
  90.       
  91.       public function set minimum(param1:Number) : void
  92.       {
  93.          _minimum = param1;
  94.          this.value = Math.max(param1,this.value);
  95.          invalidate(InvalidationType.DATA);
  96.       }
  97.       
  98.       public function get maximum() : Number
  99.       {
  100.          return _maximum;
  101.       }
  102.       
  103.       protected function positionThumb() : void
  104.       {
  105.          thumb.x = (_direction == SliderDirection.VERTICAL ? maximum - minimum - value : value - minimum) / (maximum - minimum) * _width;
  106.       }
  107.       
  108.       protected function clearTicks() : void
  109.       {
  110.          if(!tickContainer || !tickContainer.parent)
  111.          {
  112.             return;
  113.          }
  114.          removeChild(tickContainer);
  115.       }
  116.       
  117.       protected function onTrackClick(param1:MouseEvent) : void
  118.       {
  119.          calculateValue(track.mouseX,InteractionInputType.MOUSE,SliderEventClickTarget.TRACK);
  120.          if(!liveDragging)
  121.          {
  122.             dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,SliderEventClickTarget.TRACK,InteractionInputType.MOUSE));
  123.          }
  124.       }
  125.       
  126.       public function set maximum(param1:Number) : void
  127.       {
  128.          _maximum = param1;
  129.          this.value = Math.min(param1,this.value);
  130.          invalidate(InvalidationType.DATA);
  131.       }
  132.       
  133.       public function get liveDragging() : Boolean
  134.       {
  135.          return _liveDragging;
  136.       }
  137.       
  138.       protected function doDrag(param1:MouseEvent) : void
  139.       {
  140.          var _loc2_:Number = NaN;
  141.          var _loc3_:Number = NaN;
  142.          _loc2_ = _width / snapInterval;
  143.          _loc3_ = track.mouseX;
  144.          calculateValue(_loc3_,InteractionInputType.MOUSE,SliderEventClickTarget.THUMB);
  145.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_DRAG,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  146.       }
  147.       
  148.       override protected function keyDownHandler(param1:KeyboardEvent) : void
  149.       {
  150.          var _loc2_:uint = 0;
  151.          var _loc3_:Number = NaN;
  152.          var _loc4_:* = false;
  153.          if(!enabled)
  154.          {
  155.             return;
  156.          }
  157.          _loc2_ = snapInterval > 0 ? uint(snapInterval) : 1;
  158.          _loc4_ = direction == SliderDirection.HORIZONTAL;
  159.          if(param1.keyCode == Keyboard.DOWN && !_loc4_ || param1.keyCode == Keyboard.LEFT && _loc4_)
  160.          {
  161.             _loc3_ = value - _loc2_;
  162.          }
  163.          else if(param1.keyCode == Keyboard.UP && !_loc4_ || param1.keyCode == Keyboard.RIGHT && _loc4_)
  164.          {
  165.             _loc3_ = value + _loc2_;
  166.          }
  167.          else if(param1.keyCode == Keyboard.PAGE_DOWN && !_loc4_ || param1.keyCode == Keyboard.HOME && _loc4_)
  168.          {
  169.             _loc3_ = minimum;
  170.          }
  171.          else if(param1.keyCode == Keyboard.PAGE_UP && !_loc4_ || param1.keyCode == Keyboard.END && _loc4_)
  172.          {
  173.             _loc3_ = maximum;
  174.          }
  175.          if(!isNaN(_loc3_))
  176.          {
  177.             param1.stopPropagation();
  178.             doSetValue(_loc3_,InteractionInputType.KEYBOARD,null,param1.keyCode);
  179.          }
  180.       }
  181.       
  182.       override public function set enabled(param1:Boolean) : void
  183.       {
  184.          if(enabled == param1)
  185.          {
  186.             return;
  187.          }
  188.          super.enabled = param1;
  189.          track.enabled = thumb.enabled = param1;
  190.       }
  191.       
  192.       protected function thumbPressHandler(param1:MouseEvent) : void
  193.       {
  194.          stage.addEventListener(MouseEvent.MOUSE_MOVE,doDrag,false,0,true);
  195.          stage.addEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler,false,0,true);
  196.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_PRESS,value,InteractionInputType.MOUSE,SliderEventClickTarget.THUMB));
  197.       }
  198.       
  199.       public function get snapInterval() : Number
  200.       {
  201.          return _snapInterval;
  202.       }
  203.       
  204.       protected function thumbReleaseHandler(param1:MouseEvent) : void
  205.       {
  206.          stage.removeEventListener(MouseEvent.MOUSE_MOVE,doDrag);
  207.          stage.removeEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler);
  208.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_RELEASE,value,InteractionInputType.MOUSE,SliderEventClickTarget.THUMB));
  209.          dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  210.       }
  211.       
  212.       public function set liveDragging(param1:Boolean) : void
  213.       {
  214.          _liveDragging = param1;
  215.       }
  216.       
  217.       public function set value(param1:Number) : void
  218.       {
  219.          doSetValue(param1);
  220.       }
  221.       
  222.       public function set direction(param1:String) : void
  223.       {
  224.          var _loc2_:* = false;
  225.          _direction = param1;
  226.          _loc2_ = _direction == SliderDirection.VERTICAL;
  227.          if(isLivePreview)
  228.          {
  229.             if(_loc2_)
  230.             {
  231.                setScaleY(-1);
  232.                y = track.height;
  233.             }
  234.             else
  235.             {
  236.                setScaleY(1);
  237.                y = 0;
  238.             }
  239.             positionThumb();
  240.             return;
  241.          }
  242.          if(_loc2_ && componentInspectorSetting)
  243.          {
  244.             if(rotation % 90 == 0)
  245.             {
  246.                setScaleY(-1);
  247.             }
  248.          }
  249.          if(!componentInspectorSetting)
  250.          {
  251.             rotation = _loc2_ ? 90 : 0;
  252.          }
  253.       }
  254.       
  255.       public function set tickInterval(param1:Number) : void
  256.       {
  257.          _tickInterval = param1;
  258.          invalidate(InvalidationType.SIZE);
  259.       }
  260.       
  261.       override public function get enabled() : Boolean
  262.       {
  263.          return super.enabled;
  264.       }
  265.       
  266.       override protected function draw() : void
  267.       {
  268.          if(isInvalid(InvalidationType.STYLES))
  269.          {
  270.             setStyles();
  271.             invalidate(InvalidationType.SIZE,false);
  272.          }
  273.          if(isInvalid(InvalidationType.SIZE))
  274.          {
  275.             track.setSize(_width,track.height);
  276.             track.drawNow();
  277.             thumb.drawNow();
  278.          }
  279.          if(tickInterval > 0)
  280.          {
  281.             drawTicks();
  282.          }
  283.          else
  284.          {
  285.             clearTicks();
  286.          }
  287.          positionThumb();
  288.          super.draw();
  289.       }
  290.       
  291.       override protected function configUI() : void
  292.       {
  293.          super.configUI();
  294.          thumb = new BaseButton();
  295.          thumb.setSize(13,13);
  296.          thumb.autoRepeat = false;
  297.          addChild(thumb);
  298.          thumb.addEventListener(MouseEvent.MOUSE_DOWN,thumbPressHandler,false,0,true);
  299.          track = new BaseButton();
  300.          track.move(0,0);
  301.          track.setSize(80,4);
  302.          track.autoRepeat = false;
  303.          track.useHandCursor = false;
  304.          track.addEventListener(MouseEvent.CLICK,onTrackClick,false,0,true);
  305.          addChildAt(track,0);
  306.       }
  307.       
  308.       public function set snapInterval(param1:Number) : void
  309.       {
  310.          _snapInterval = param1;
  311.       }
  312.       
  313.       public function get value() : Number
  314.       {
  315.          return _value;
  316.       }
  317.       
  318.       public function get direction() : String
  319.       {
  320.          return _direction;
  321.       }
  322.       
  323.       public function get tickInterval() : Number
  324.       {
  325.          return _tickInterval;
  326.       }
  327.       
  328.       override public function setSize(param1:Number, param2:Number) : void
  329.       {
  330.          if(_direction == SliderDirection.VERTICAL && !isLivePreview)
  331.          {
  332.             super.setSize(param2,param1);
  333.          }
  334.          else
  335.          {
  336.             super.setSize(param1,param2);
  337.          }
  338.          invalidate(InvalidationType.SIZE);
  339.       }
  340.       
  341.       protected function drawTicks() : void
  342.       {
  343.          var _loc1_:Number = NaN;
  344.          var _loc2_:Number = NaN;
  345.          var _loc3_:Number = NaN;
  346.          var _loc4_:uint = 0;
  347.          var _loc5_:DisplayObject = null;
  348.          clearTicks();
  349.          tickContainer = new Sprite();
  350.          _loc1_ = maximum < 1 ? tickInterval / 100 : tickInterval;
  351.          _loc2_ = (maximum - minimum) / _loc1_;
  352.          _loc3_ = _width / _loc2_;
  353.          _loc4_ = 0;
  354.          while(_loc4_ <= _loc2_)
  355.          {
  356.             (_loc5_ = getDisplayObjectInstance(getStyleValue("tickSkin"))).x = _loc3_ * _loc4_;
  357.             _loc5_.y = track.y - _loc5_.height - 2;
  358.             tickContainer.addChild(_loc5_);
  359.             _loc4_++;
  360.          }
  361.          addChild(tickContainer);
  362.       }
  363.       
  364.       protected function calculateValue(param1:Number, param2:String, param3:String, param4:int = undefined) : void
  365.       {
  366.          var _loc5_:Number = NaN;
  367.          _loc5_ = param1 / _width * (maximum - minimum);
  368.          if(_direction == SliderDirection.VERTICAL)
  369.          {
  370.             _loc5_ = maximum - _loc5_;
  371.          }
  372.          else
  373.          {
  374.             _loc5_ = minimum + _loc5_;
  375.          }
  376.          doSetValue(_loc5_,param2,param3,param4);
  377.       }
  378.       
  379.       protected function getPrecision(param1:Number) : Number
  380.       {
  381.          var _loc2_:String = null;
  382.          _loc2_ = param1.toString();
  383.          if(_loc2_.indexOf(".") == -1)
  384.          {
  385.             return 0;
  386.          }
  387.          return _loc2_.split(".").pop().length;
  388.       }
  389.       
  390.       protected function doSetValue(param1:Number, param2:String = null, param3:String = null, param4:int = undefined) : void
  391.       {
  392.          var _loc5_:Number = NaN;
  393.          var _loc6_:Number = NaN;
  394.          var _loc7_:Number = NaN;
  395.          var _loc8_:Number = NaN;
  396.          var _loc9_:Number = NaN;
  397.          _loc5_ = _value;
  398.          if(_snapInterval != 0 && _snapInterval != 1)
  399.          {
  400.             _loc6_ = Math.pow(10,getPrecision(snapInterval));
  401.             _loc7_ = _snapInterval * _loc6_;
  402.             _loc8_ = Math.round(param1 * _loc6_);
  403.             param1 = (_loc9_ = Math.round(_loc8_ / _loc7_) * _loc7_) / _loc6_;
  404.             _value = Math.max(minimum,Math.min(maximum,param1));
  405.          }
  406.          else
  407.          {
  408.             _value = Math.max(minimum,Math.min(maximum,Math.round(param1)));
  409.          }
  410.          if(_loc5_ != _value && (liveDragging && param3 != null || param2 == InteractionInputType.KEYBOARD))
  411.          {
  412.             dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,param3,param2,param4));
  413.          }
  414.          positionThumb();
  415.       }
  416.       
  417.       protected function setStyles() : void
  418.       {
  419.          copyStylesToChild(thumb,THUMB_STYLES);
  420.          copyStylesToChild(track,TRACK_STYLES);
  421.       }
  422.    }
  423. }
  424.